package com.jinSgr.md5_sm2_encryption;

import android.os.Build;

import androidx.annotation.RequiresApi;

import com.jinSgr.md5_sm2_encryption.sm2.SM2EncDecUtils;
import com.jinSgr.md5_sm2_encryption.sm2.Util;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class SunvuaEncryptionUtils {
    @RequiresApi(api = Build.VERSION_CODES.O)
    public  static  String md5bybase64bySm2(String  plainText,String pubk){
      
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] sourceDataMd5 = md5.digest(plainText.getBytes(StandardCharsets.UTF_8));

//            System.out.println("MD5"+ Util.byteToHex(sourceDataMd5));
            String md5String =Util.byteToHex(sourceDataMd5);
            String mdbase = Base64.getEncoder().encodeToString(md5String.getBytes(StandardCharsets.UTF_8));
            byte[] sourceData = mdbase.getBytes(StandardCharsets.UTF_8);
//            System.out.println("加密: ");
             plainText = SM2EncDecUtils.encrypt(Util.hexToByte(pubk), sourceData);
//            System.out.println(plainText);
        
        } catch (NoSuchAlgorithmException | IOException e) {
            e.printStackTrace();
        }finally {
            return plainText;
        }
        

    }
    
}
